﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EntityExtensions.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   The entity extensions.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.DataAccess.Linq
{
    #region Imports

    using System.Collections.Generic;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Linq;

    #endregion

    /// <summary>
    /// The entity extensions.
    /// </summary>
    public static class EntityExtensions
    {
        /// <summary>
        /// The set all modified.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        public static void SetAllModified<T>(this T entity, ObjectContext context) where T : IEntityWithKey
        {
            ObjectStateEntry stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey);

            IEnumerable<string> propertyNameList = stateEntry.CurrentValues.DataRecordInfo.FieldMetadata.Select(pn => pn.FieldType.Name);

            foreach (string propName in propertyNameList)
            {
                stateEntry.SetModifiedProperty(propName);
            }
        }
    }
}